package com.yunxi.module.system.controller.admin.notice;

import cn.hutool.core.lang.Assert;
import com.yunxi.framework.common.enums.UserTypeEnum;
import com.yunxi.framework.common.pojo.CommonResult;
import com.yunxi.framework.common.pojo.PageResult;
import com.yunxi.framework.common.util.object.BeanUtils;
import com.yunxi.module.infra.api.websocket.WebSocketSenderApi;
import com.yunxi.module.system.model.notice.NoticeDO;
import com.yunxi.module.system.service.notice.NoticeService;
import com.yunxi.module.system.vo.admin.notify.message.notice.NoticePageReqVO;
import com.yunxi.module.system.vo.admin.notify.message.notice.NoticeRespVO;
import com.yunxi.module.system.vo.admin.notify.message.notice.NoticeSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 管理后台 - 通知公告
 * </p>
 *
 * @author lidy
 * @since 2024-06-05
 */
@Tag(name = "管理后台 - 通知公告")
@RestController
@RequestMapping("/system/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @Resource
    private WebSocketSenderApi webSocketSenderApi;

    @PostMapping("/push")
    @Operation(summary = "推送通知公告", description = "只发送给 WebSocket 连接在线的用户")
    @PreAuthorize("@ss.hasPermission('system:notice:update')")
    @Parameter(name = "noticeId", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> pushNotice(@RequestParam("noticeId") Long noticeId) {
        // 获得通知公告
        NoticeDO notice = noticeService.getNotice(noticeId);
        // 断言
        Assert.notNull(notice, "公告不能为空");
        // 通过 WebSocket 推送给在线的用户
        webSocketSenderApi.sendObject(UserTypeEnum.ADMIN.getValue(), "notice-push", notice);
        return CommonResult.success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通知公告")
    @PreAuthorize("@ss.hasPermission('system:notice:delete')")
    @Parameter(name = "noticeId", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> deleteNotice(@RequestParam("noticeId") Long noticeId) {
        noticeService.deleteNotice(noticeId);
        return CommonResult.success(true);
    }

    @PostMapping("/create")
    @Operation(summary = "创建通知公告")
    @PreAuthorize("@ss.hasPermission('system:notice:create')")
    public CommonResult<Long> createNotice(@Valid @RequestBody NoticeSaveReqVO createReqVO) {
        Long noticeId = noticeService.createNotice(createReqVO);
        return CommonResult.success(noticeId);
    }

    @PutMapping("/update")
    @Operation(summary = "修改通知公告")
    @PreAuthorize("@ss.hasPermission('system:notice:update')")
    public CommonResult<Boolean> updateNotice(@Valid @RequestBody NoticeSaveReqVO updateReqVO) {
        noticeService.updateNotice(updateReqVO);
        return CommonResult.success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通知公告")
    @PreAuthorize("@ss.hasPermission('system:notice:query')")
    @Parameter(name = "noticeId", description = "编号", required = true, example = "1024")
    public CommonResult<NoticeRespVO> getNotice(@RequestParam("noticeId") Long noticeId) {
        NoticeDO noticeDO = noticeService.getNotice(noticeId);
        return CommonResult.success(BeanUtils.toBean(noticeDO, NoticeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获取通知公告列表")
    @PreAuthorize("@ss.hasPermission('system:notice:query')")
    public CommonResult<PageResult<NoticeRespVO>> getNoticePage(@Valid NoticePageReqVO reqVO) {
        PageResult<NoticeDO> pageResult = noticeService.getNoticePage(reqVO);
        return CommonResult.success(BeanUtils.toBean(pageResult, NoticeRespVO.class));
    }

}
